package com.hksj.Intelligentmanagement.auth.service;

import com.hksj.Intelligentmanagement.auth.entity.LoginUser;
import com.hksj.Intelligentmanagement.auth.entity.User;
import com.hksj.Intelligentmanagement.auth.manager.TokenManager;
import com.hksj.Intelligentmanagement.auth.vo.LoginInfoVo;
import com.hksj.Intelligentmanagement.auth.vo.LoginUserInfoVo;
import com.hksj.Intelligentmanagement.auth.vo.MenuVo;
import com.hksj.Intelligentmanagement.common.Constants;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.LoginDTO;
import com.hksj.Intelligentmanagement.utils.RSAUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;
import java.util.Objects;

@Service
public class LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private TokenManager tokenManager;

    @Value("${rsa.private}")
    private String privateSecurity;

    @Autowired
    private MenuService menuService;


    public Result login(LoginDTO dto) {
        //rsa解密
        String decrypt = RSAUtils.decrypt(dto.getPassword(), privateSecurity);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(dto.getUsername(), decrypt);
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("用户名或密码错误");
        }
        //使用userid生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();

        String jwt = tokenManager.createToken(loginUser, 8);

        //把token响应给前端
        LoginInfoVo loginInfoVo = getLoginInfo(loginUser);
        loginInfoVo.setToken(jwt);

        return Result.get(Constants.RESULT_CODE_SUCCESS, "登陆成功", loginInfoVo);
    }

    private LoginInfoVo getLoginInfo(LoginUser loginUser){
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        //用户信息
        User user = loginUser.getUser();
        LoginUserInfoVo loginUserInfo = new LoginUserInfoVo();
        BeanUtils.copyProperties(user,loginUserInfo);
        loginInfoVo.setUserInfo(loginUserInfo);

        //角色信息
        loginInfoVo.setRoles(loginUser.getRoles());

        //菜单权限
        List<MenuVo> userMenuTree = menuService.getUserMenuTree(loginUser.getUser().getId());
        loginInfoVo.setMenus(userMenuTree);
        return loginInfoVo;
    }


    public Result logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        tokenManager.logout(loginUser);
        return Result.get(Constants.RESULT_CODE_SUCCESS, "退出成功");
    }

    public Result kick(String userId) {
        tokenManager.kick(userId);
        return Result.ok();
    }

    public LoginInfoVo getUserInfo(HttpServletRequest request, Principal principal) {
        String token = request.getHeader("token");
        LoginUser loginUser = (LoginUser) ((UsernamePasswordAuthenticationToken) principal).getPrincipal();
        LoginInfoVo loginInfoVo = getLoginInfo(loginUser);
        loginInfoVo.setToken(token);
        return loginInfoVo;
    }
}
 